package com.learn.lb.spark.sql


import org.apache.spark.sql.SparkSession

/**
 * SparkSession的使用
 *
 * @author laibo
 * @since 2019/8/9 12:52
 */
object SparkSessionDemo {

  def main(args: Array[String]): Unit = {
    val sparkSession = SparkSession.builder().appName("SparkSessionDemo").master("local[2]").getOrCreate()
    val student = sparkSession.read.json("E:\\idea\\workspace\\bd-learn\\spark-learn\\src\\main\\resources\\student.json")
    student.show(20)
    println("=" * 20)
    student.createOrReplaceTempView("t_student")
    sparkSession.sql("select * from t_student as t").show()
    sparkSession.stop()
    test()
  }

  /**
   * posexplode 用法实例
   */
  def test(): Unit= {
    val sparkSession = SparkSession.builder().appName("SparkSessionDemo2").master("local[2]").getOrCreate()
    val testDF = sparkSession.read.json("E:\\idea\\workspace\\spark-sql\\src\\main\\resources\\test.json")
    testDF.printSchema()
    testDF.createOrReplaceTempView("t_test")
    sparkSession.sql("select * from t_test as t lateral view posexplode(split(t.name, ',')) tn as posName, tName  " +
      "lateral view posexplode(split(t.value, ',')) tv as posValue, tValue  where posName = posValue").show()
    sparkSession.stop()
  }
}
